package com.struts.core.servlet;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.struts.core.annotation.Resource;
import com.struts.core.util.InstallFormUtil;
/**
 * MVC转发器
 * @author gzq
 *
 */
public class MyServlet extends HttpServlet {

	private static final long serialVersionUID = 3329559487299777329L;

	@Override
	public void init() throws ServletException {
		super.init();
	}

	@SuppressWarnings({ "deprecation", "unused", "unchecked", "rawtypes" })
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.getInitParameter("url-pattern");
		Enumeration<Servlet> ee = this.getServletContext().getServlets();
		while (ee.hasMoreElements()) {
			Servlet s = ee.nextElement();
			System.out.println(s);
		}
		String action = request.getServletPath();
		ServletConfig con = this.getServletConfig();
		ServletContext context = this.getServletContext();
		Map<String, Object> map = (Map<String, Object>) context
				.getAttribute("strutsMap");
		String value = (String) map.get(action);
		String[] values = value.split("\\|\\|");
		String className = values[0];// 第一个参数是类名
		String methodName = values[1];// 第二个参数是方法
		String[] parasName = values[2].split(",");// 第二个参数是方法参数
		Class[] paras = new Class[parasName.length];
		for (int index = 0; index < parasName.length; index++) { 
			try {
				paras[index] = Class.forName(parasName[index]);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		Object myAction;
		Class clazz;
		Object rs = null;
		try {
			clazz = Class.forName(className);
			myAction = clazz.newInstance();
			
			//以下是把  service层和dao层注入进来
			
			// 为每个aciton里面的service 赋值
			Field[] fields = clazz.getDeclaredFields();
			for (Field field : fields) {
				Resource res = field.getAnnotation(Resource.class);
				if (res != null) {
					String classFieldName = (String) map.get(className
							+ field.getName());
					System.out.println("classFieldName==" + classFieldName);
					field.setAccessible(true);

					Class serviceClazz = Class.forName(classFieldName);
					Object serviceObject = serviceClazz.newInstance();
					// 给service中的属性赋值
					Field[] serviceFields = serviceClazz.getDeclaredFields();
					for (Field sf : serviceFields) {
						Resource sres = sf.getAnnotation(Resource.class);
						if (sres != null) {
							String sClassFieldName = (String) map.get(classFieldName
									+ sf.getName());
							
							sf.setAccessible(true);
							sf.set(serviceObject, Class.forName(sClassFieldName).newInstance());
							sf.setAccessible(false);
						}
					}

					field.set(myAction, serviceObject);
					field.setAccessible(false);
				}

			}
			//以下是把  service层和dao层注入进来--结束

			Method method = clazz.getDeclaredMethod(methodName, paras);// 获取方法
			Object[] parasInstaces = new Object[paras.length];
			for (int i = 0; i < paras.length; i++) {
				// 装配方法参数
				Object paraInstance = InstallFormUtil
						.install(paras[i], request);
				parasInstaces[i] = paraInstance;
			}
			// 执行方法
			rs = method.invoke(myAction, parasInstaces);
		} catch (Exception e) {
			e.printStackTrace();
		}
		RequestDispatcher dis = request.getRequestDispatcher(rs.toString());
		dis.forward(request, response);

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}

}
